﻿Imports XCLI_API
Imports System.IO

Public Class Plugin
    Implements XCLIPlugin


    ''BEGIN: Properties.

    'This provides the common name. This is used as a title for the pluin
    Public ReadOnly Property CommonName() As String Implements XCLIPlugin.CommonName
        Get
            'return the name.
            Return "XCLI Core"
        End Get
    End Property

    'This is a description of the plugin. This can be any length and is designed to describe to a user
    'what the plugin does if this plugin is read by another.
    Public ReadOnly Property Description() As String Implements XCLIPlugin.Description
        Get
            Return "Generic Core plugin, controlling the main XCLI commands"
        End Get
    End Property

    'This is the full plugin name. All plugins must have unique names.
    'Creating unique names:
    'A good practice to follow is putting the author name/company name in the fullname such as:
    ' "com.tom.test.dll"
    ' [TYPE].[AUTHOR].[COMMON_NAME].DLL
    Public ReadOnly Property Fullname() As String Implements XCLIPlugin.Fullname
        Get
            Return "xcli.core.dll"
        End Get
    End Property

    'This denotes the plugin author
    Public ReadOnly Property Author() As String Implements XCLIPlugin.Author
        Get
            Return "XCLI"
        End Get
    End Property

    'This is important. Every time you update your plugin you MUST increment the build.
    'You can increment it by any number you desire, but each version must have a larger build than the last
    'This is used for dependency checking when installing plugins.
    Public ReadOnly Property Build() As String Implements XCLIPlugin.Build
        Get
            Return My.Application.Info.Version.Build
        End Get
    End Property

    'This lists the required DLL's to be installed. The format is as follows:
    ' "[FULLNAME]:[BUILD];"
    ' This specifies a specific build that must be installed to run this DLL, however you
    ' can specify the build must be "greater than" a certain number by typing:
    ' "[FULLNAME],[BUILD]+;"

    ' EG: "com.xcli.plugin1.dll:1023;"   - This specifies that build 1023 of the com.xcli.plugin1.dll must be installed
    ' EG: "com.xcli.plugin2.dll:1546+;"  - This specifies that any build greater than 1546 of com.xcli.plugin2.dll must be installed
    Public ReadOnly Property Dependencies() As String Implements XCLIPlugin.Dependencies
        Get
            Return ""
        End Get
    End Property

    'This denotes the keywords associated with this plugin. These keywords tell XCLI that this plugin needs to be called if the user types a word.
    ' For this plugin "test" has been added as the keyword. If the user types any command that bgins with "test " then the command will be passed
    'to this plugin. EG if the user typed:
    ' "test startplugin"   - This would tell XCLI to send the command to this plugin, where it is handled.
    Public ReadOnly Property Keywords() As String Implements XCLIPlugin.Keywords
        Get
            Return "cd,ls"
        End Get
    End Property



    'This is the actual interface. When any of the keywords (described above) are typed into XCLI, the whole commands string [command] and the
    'environmental variables [env()] of XCLI are passed to this plugin. The environmental variables can be changed or modified from within this plugin,
    'and those changes appear when any plugin uses the environmental variables.
    Public Sub CONSTRUCT(ByVal command As String, ByRef env() As String) Implements XCLIPlugin.CONSTRUCT
        Dim CommandArr() As String = command.Split(" ")

        Select Case CommandArr(0)
            Case Is = "cd"
                cmd_cd(env, CommandArr(1))
            Case Is = "ls"
                cmd_ls(env())
        End Select
    End Sub

    Public Sub HELP(ByVal command As String, ByRef env() As String) Implements XCLIPlugin.HELP
        Console.WriteLine("Commands ""cd"", ""ls""")
    End Sub


    Private Sub cmd_cd(ByRef env() As String, ByVal newDir As String)
        Try
            Dim notFound As Boolean = False
            If Not newDir.EndsWith("\") Then

                newDir = newDir & "\"
            End If
            If newDir.Substring(1, 2) = ":\" Then
                If File.Exists(newDir) Then
                    env(0) = newDir
                Else
                    notFound = True
                End If

            Else
                If Not newDir.StartsWith("\") Then
                    If File.Exists(env(0) & newDir) Then
                        env(0) = env(0) & newDir
                    Else
                        notFound = True
                    End If
                Else
                    If File.Exists(env(0) & newDir.Substring(1, Len(newDir) - 1)) Then
                        env(0) = env(0) & newDir.Substring(1, Len(newDir) - 1)
                    Else
                        notFound = True
                    End If

                End If
            End If
            If notFound = True Then
                Throw New Exception("Invalid Path.")
            End If
        Catch ex As Exception
            Console.WriteLine("Error: " & ex.Message.ToString)
        End Try
    End Sub

    Private Sub cmd_ls(ByRef env() As String)
        Dim Root As New DirectoryInfo(env(0))

        Dim Files As FileInfo() = Root.GetFiles("*.*")
        Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")

        Console.WriteLine("Root Files")
        Dim Filename As FileInfo

        For Each Filename In Files
            Try
                Console.WriteLine(Filename.FullName)
            Catch E As Exception
                Console.WriteLine("Error accessing File")
            End Try
        Next

    End Sub



End Class
